package com.uwlaser.modules.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.modules.system.domain.SysLogsPlant;
import com.uwlaser.modules.system.service.SysLogsPlantService;
import com.uwlaser.modules.system.service.dto.SysLogsPlantDto;
import com.uwlaser.modules.system.service.dto.SysLogsPlantQueryParam;
import com.uwlaser.modules.system.service.mapper.SysLogsPlantMapper;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author liuzhihui
* @date 2022-04-04
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = SysLogsPlantService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SysLogsPlantServiceImpl extends CommonServiceImpl<SysLogsPlantMapper, SysLogsPlant> implements SysLogsPlantService {

    // private final RedisUtils redisUtils;
    private final SysLogsPlantMapper sysLogsPlantMapper;

    @Override
    public PageInfo<SysLogsPlantDto> queryAll(SysLogsPlantQueryParam query, Pageable pageable) {
        IPage<SysLogsPlant> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<SysLogsPlant> page = sysLogsPlantMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, SysLogsPlantDto.class);
    }

    @Override
    public List<SysLogsPlantDto> queryAll(SysLogsPlantQueryParam query){
        return ConvertUtil.convertList(sysLogsPlantMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), SysLogsPlantDto.class);
    }

    @Override
    public SysLogsPlant getById(Integer id) {
        return sysLogsPlantMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public SysLogsPlantDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), SysLogsPlantDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(SysLogsPlantDto resources) {
        SysLogsPlant entity = ConvertUtil.convert(resources, SysLogsPlant.class);
        return sysLogsPlantMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(SysLogsPlantDto resources){
        SysLogsPlant entity = ConvertUtil.convert(resources, SysLogsPlant.class);
        int ret = sysLogsPlantMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return sysLogsPlantMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<SysLogsPlantDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (SysLogsPlantDto sysLogsPlant : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("请求接口", sysLogsPlant.getRequestclassname());
              map.put("请求内容", sysLogsPlant.getRequestcontent());
              map.put("错误信息", sysLogsPlant.getErrormsg());
              map.put("重试次数", sysLogsPlant.getRetrynum());
              map.put("创建时间", sysLogsPlant.getCreatetime());
              map.put("请求方法", sysLogsPlant.getRequestmethodname());
              map.put("请求接口名称", sysLogsPlant.getRequestportname());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
